package com.example.lxj.base.service.impl;

import com.example.lxj.base.service.PermissionService;
import com.example.lxj.base.utils.RedisUtils;
import com.example.lxj.system.entity.SysUserToken;
import com.example.lxj.system.service.ISysUserTokenService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private ISysUserTokenService sysUserTokenService;
    @Override
    public boolean checkPermission(String permissionValue, HttpServletRequest request) {
        // 获取token
        String token = request.getHeader("token");
        // 获取用户id
        SysUserToken sysUserToken = sysUserTokenService.selectSysUserIdByUserToken(token);
        // 从缓存中获取permission
        Object permission = redisUtils.get(sysUserToken.getUserId().toString());
        List<?> permissionList = (List<?>) permission;
        if(permissionList.contains("*:*") || permissionList.contains(permissionValue)){
            return true;
        }else{
            return false;
        }
    }
}
